package org.jivesoftware.smack.packet;

import org.jivesoftware.smack.util.StringUtils;

public abstract class IQ extends Packet
{
  private Type type = Type.GET;

  public static IQ createErrorResponse(IQ paramIQ, XMPPError paramXMPPError)
  {
    if ((paramIQ.getType() != Type.GET) && (paramIQ.getType() != Type.SET))
      throw new IllegalArgumentException("IQ must be of type 'set' or 'get'. Original IQ: " + paramIQ.toXML());
    2 local2 = new IQ(paramIQ)
    {
      public String getChildElementXML()
      {
        return this.val$request.getChildElementXML();
      }
    };
    local2.setType(Type.ERROR);
    local2.setPacketID(paramIQ.getPacketID());
    local2.setFrom(paramIQ.getTo());
    local2.setTo(paramIQ.getFrom());
    local2.setError(paramXMPPError);
    return local2;
  }

  public static IQ createResultIQ(IQ paramIQ)
  {
    if ((paramIQ.getType() != Type.GET) && (paramIQ.getType() != Type.SET))
      throw new IllegalArgumentException("IQ must be of type 'set' or 'get'. Original IQ: " + paramIQ.toXML());
    1 local1 = new IQ()
    {
      public String getChildElementXML()
      {
        return null;
      }
    };
    local1.setType(Type.RESULT);
    local1.setPacketID(paramIQ.getPacketID());
    local1.setFrom(paramIQ.getTo());
    local1.setTo(paramIQ.getFrom());
    return local1;
  }

  public abstract String getChildElementXML();

  public Type getType()
  {
    return this.type;
  }

  public void setType(Type paramType)
  {
    if (paramType == null)
      this.type = Type.GET;
    while (true)
    {
      return;
      this.type = paramType;
    }
  }

  public String toXML()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("<iq ");
    if (getPacketID() != null)
      localStringBuilder.append("id=\"" + getPacketID() + "\" ");
    if (getTo() != null)
      localStringBuilder.append("to=\"").append(StringUtils.escapeForXML(getTo())).append("\" ");
    if (getFrom() != null)
      localStringBuilder.append("from=\"").append(StringUtils.escapeForXML(getFrom())).append("\" ");
    if (this.type == null)
      localStringBuilder.append("type=\"get\">");
    while (true)
    {
      String str = getChildElementXML();
      if (str != null)
        localStringBuilder.append(str);
      XMPPError localXMPPError = getError();
      if (localXMPPError != null)
        localStringBuilder.append(localXMPPError.toXML());
      localStringBuilder.append("</iq>");
      return localStringBuilder.toString();
      localStringBuilder.append("type=\"").append(getType()).append("\">");
    }
  }

  public static class Type
  {
    public static final Type ERROR;
    public static final Type GET = new Type("get");
    public static final Type RESULT;
    public static final Type SET = new Type("set");
    private String value;

    static
    {
      RESULT = new Type("result");
      ERROR = new Type("error");
    }

    private Type(String paramString)
    {
      this.value = paramString;
    }

    public static Type fromString(String paramString)
    {
      Type localType;
      if (paramString == null)
        localType = null;
      while (true)
      {
        while (true)
        {
          String str;
          while (true)
          {
            while (true)
            {
              while (true)
              {
                return localType;
                str = paramString.toLowerCase();
                if (!(GET.toString().equals(str)))
                  break;
                localType = GET;
              }
              if (!(SET.toString().equals(str)))
                break;
              localType = SET;
            }
            if (!(ERROR.toString().equals(str)))
              break;
            localType = ERROR;
          }
          if (!(RESULT.toString().equals(str)))
            break;
          localType = RESULT;
        }
        localType = null;
      }
    }

    public String toString()
    {
      return this.value;
    }
  }
}